﻿@using MvcControls
@using MvcControls.Controls.Base
@using MvcControls.Controls.Button
@using MvcControls.Controls.DataGrid
@using MvcControls.Controls.Parameters
@using SDM.Domain.Models
@using ViewTexts = SDM.Main.Areas.App.Views.Purchase.Texts;
@using ModelTexts = SDM.Domain.Models.PurchaseTransactionModel.Texts;
@using DetailModelTexts = SDM.Domain.Models.PurchaseTransactionDetailModel.Texts;
@inherits WebViewPage<PurchaseTransactionModel>, ILocalizable<ViewTexts>, ILocalizable<ModelTexts>, ILocalizable<DetailModelTexts>
@{
    var isAddMode = Model != null && Model.IsNew;
    ViewBag.Title = isAddMode ? this.Localize<ViewTexts>(t => t.AddTransaction) : this.Localize<ViewTexts>(t => t.EditTransaction);
}
@using (Html.BeginForm())
{
    <fieldset>
        <legend>@(this.Localize<ViewTexts>(t => t.HeaderInformation))</legend>
        <div>
            @Html.LabelFor(t => t.Time, this.Localize<ModelTexts>(t => t.Time))
            @Html.TextBoxFor(t => t.Time)
            @*@Html.LabelFor(t => t.Supplier, this.Localize<ModelTexts>(t => t.Supplier))
        @Html.TextBoxFor(t => t.Supplier)*@
            @Html.Custom().TextBox().Bind(t => t.Supplier).Settings(
                s =>
                    {
                        s.LabelText = this.Localize<ModelTexts>(t => t.Supplier);
                        s.AutoCompleteDataSource = new ActionParameter("GetSuppliers");
                    })
        </div>
        <div>
            @Html.ValidationSummary()
            @Html.Custom().Render((IHtmlControl)ViewBag.ResultMessage)
        </div>
    </fieldset>
    <fieldset>
        <legend>@(this.Localize<ViewTexts>(t => t.DetailInformation))</legend>
        @(Html.Custom().Grid(t => t.Details)
        .AddPropertyColumn(this.Localize<DetailModelTexts>(t => t.ItemName), t => t.ItemName)
        .AddPropertyColumn(this.Localize<DetailModelTexts>(t => t.Quantity), t => t.Quantity)        
        .Settings(s =>
                      {
                          s.EditModule = new DataGridInlineFormEditModule<PurchaseTransactionDetailModel>
                                             {
                                                 NewLineContentUrl = new ActionParameter("AddOrEditTransactionDetail"),
                                                 RowIdPropertySelector = t => t.ID
                                             };                          
                      })
              )
        <script type="text/javascript">
            // override previous grid
            var grid = Grid.Latest;
            grid.OnFormatData = function (name, value)
            {
                // validate
                if (name == "@(Html.IdForProperty<PurchaseTransactionDetailModel, string>(t => t.ItemName))")
                {
                    // empty?
                    if (value.length == 0)
                    {
                        // not accepted
                        return new Error("@(this.Localize<ViewTexts>(t => t.ValidationItemNameMustNotBeEmpty))");
                    }
                }
                if (name == "@(Html.IdForProperty<PurchaseTransactionDetailModel, int>(t => t.Quantity))")
                {
                    // greater than zero
                    if (value <= 0)
                    {
                        // not accepted
                        return new Error("@(this.Localize<ViewTexts>(t => t.ValidationQuantityMustBeGreaterThanZero))");
                    }
                }
                
                // return value
                return value;
            };
        </script>
    </fieldset>
        
    @Html.Custom().Button().Settings(s =>
                                         {
                                             s.Text = this.Localize<ViewTexts>(t => isAddMode ? t.Shared.Add : t.Shared.Save);
                                             s.Type = ButtonControlInfo.ButtonType.Submit;
                                         })
    @Html.Custom().Button().Settings(s =>
                                         {
                                             s.Text = this.Localize<ViewTexts>(t => t.Shared.Cancel);
                                             s.Action = new ActionParameter("Transactions");
                                         })
}